//
//  PwrdSdkApi_Android.cpp
//  clientOversea
//
//  Created by ming on 2019/1/9.
//

#include "PwrdSdkApi.hpp"
#include "cocos2d.h"
#include <jni.h>
#include "platform/android/jni/JniHelper.h"

#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"

using namespace cocos2d;

#define ChannelAPIJavaClass "com/repong/framework/ChannelAPI"

static std::string s_versionInfo = "1.0.0.1";

void PwrdSdkApi::setRemoteVersionInfo(std::string versionInfoStr){
    s_versionInfo = versionInfoStr;
}

std::string PwrdSdkApi::getRemoteVersionInfo(){
    return s_versionInfo;
}

std::string PwrdSdkApi::getRemoteAppVersion(){
    auto idx = s_versionInfo.find_last_of(".");
    std::string ret = s_versionInfo.substr(0, idx);
    return ret;
}

std::string PwrdSdkApi::getRemoteResourceVersion(){
    auto idx = s_versionInfo.find_last_of(".");
    std::string ret = s_versionInfo.substr(idx+1);
    return ret;
}

void PwrdSdkApi::jumpToAppShop(){
    JniMethodInfo t;
    if(JniHelper::getStaticMethodInfo(t, ChannelAPIJavaClass, "jumpToAppShop", "()V"))
    {
        t.env->CallStaticVoidMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
    }
}

bool PwrdSdkApi::isReview(){
    return false;
}

bool PwrdSdkApi::checkPermissions(){
    JniMethodInfo t;
    bool ret = false;
    if(JniHelper::getStaticMethodInfo(t, ChannelAPIJavaClass, "checkPermissions", "()Z"))
    {
        ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
    }
    return ret;
}

void PwrdSdkApi::requestPermissions(){
    JniMethodInfo t;
    if(JniHelper::getStaticMethodInfo(t, ChannelAPIJavaClass, "requestPermissions", "()V"))
    {
        t.env->CallStaticVoidMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
    }
}

void PwrdSdkApi::goAppSetting(){
    JniMethodInfo t;
    if(JniHelper::getStaticMethodInfo(t, ChannelAPIJavaClass, "goAppSetting", "()V"))
    {
        t.env->CallStaticVoidMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
    }
}

void PwrdSdkApi::wanmeiTrackEvent(const char* data){
    JniMethodInfo t;
    if (JniHelper::getStaticMethodInfo(t, ChannelAPIJavaClass, "wanmeiTrackEvent", "(Ljava/lang/String;)V")) {
        rapidjson::StringBuffer s;
        rapidjson::Writer<rapidjson::StringBuffer> writer(s);
        writer.StartObject();
        writer.Key("eventName");
        writer.String(data);
        writer.EndObject();
        std::string jsonStr = s.GetString();
        jstring jstr = t.env->NewStringUTF(jsonStr.c_str());
        t.env->CallStaticVoidMethod(t.classID, t.methodID, jstr);
        t.env->DeleteLocalRef(jstr);
        t.env->DeleteLocalRef(t.classID);
    }
}

void PwrdSdkApi::wanmeiGameEvent(const char* eventName, int stateIndex, const char* url, const char* errorMsg){
    JniMethodInfo t;
    if (JniHelper::getStaticMethodInfo(t, ChannelAPIJavaClass, "wanmeiGameEvent", "(Ljava/lang/String;)V")) {
        rapidjson::StringBuffer s;
        rapidjson::Writer<rapidjson::StringBuffer> writer(s);
        writer.StartObject();
        writer.Key("eventName");
        writer.String(eventName);
        writer.Key("stateIndex");
        writer.Int(stateIndex);
        writer.Key("url");
        writer.String(url);
        writer.Key("errorMsg");
        writer.String(errorMsg);
        writer.EndObject();
        std::string jsonStr = s.GetString();
        jstring jstr = t.env->NewStringUTF(jsonStr.c_str());
        t.env->CallStaticVoidMethod(t.classID, t.methodID, jstr);
        t.env->DeleteLocalRef(jstr);
        t.env->DeleteLocalRef(t.classID);
    }
}

void PwrdSdkApi::setSDKLanguage(const char* language){
    JniMethodInfo t;
    if (JniHelper::getStaticMethodInfo(t, ChannelAPIJavaClass, "setSDKLanguage", "(Ljava/lang/String;)V")){
        jstring jsonStr = t.env->NewStringUTF(language);
        t.env->CallStaticVoidMethod(t.classID, t.methodID, jsonStr);
        t.env->DeleteLocalRef(jsonStr);
        t.env->DeleteLocalRef(t.classID);
    }
}
